/* eslint-disable no-param-reassign */
import React, { Component } from 'react';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import moment from 'moment';
import { connect } from 'dva';
import router from 'umi/router';
import AddTmp from './AddTmp';

// @connect(({ rebate: { empNameList }, loading }) => ({
//   empNameList,
//   loading: loading.effects['rebate/fetchEmpNameList'],
// }))
@connect(({ rebate: { empNameList, serviceDictList, insUpdateVo, accuUpdateVo }, loading }) => ({
  empNameList,
  serviceDictList,
  loading: loading.effects['rebate/fetchEmpNameList'],
  insUpdateVo,
  accuUpdateVo,
}))
@Form.create()
class AddInfor extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    const { location } = this.props;
    const { type, refundInfoId } = location.query;
    const { dispatch } = this.props;
    this.getServiceDicList(type);
    if (refundInfoId) {
      dispatch({
        type: Number(type) === 1 ? 'rebate/fetchInsUpdateInfo' : 'rebate/fetchAccuUpdateInfo',
        payload: {
          refundInfoId,
        },
      });
    } else {
      dispatch({
        type: 'rebate/clearUpdateInfo',
        payload: {},
      });
    }
  }

  cancle = () => {
    window.history.back();
  };

  empData = data => {
    const { dispatch } = this.props;
    dispatch({
      type: 'rebate/getEmpNameList',
      payload: data,
    });
  };

  empSearch = values => {
    const { dispatch } = this.props;
    dispatch({
      type: 'rebate/fetchEmpNameList',
      payload: values,
    });
  };

  // idIconClick = values => {
  //   const { dispatch } = this.props;
  //   dispatch({
  //     type: 'rebate/fetchEmpNameList',
  //     payload: values,
  //   });
  //  };

  valiData = ref => {
    this.child = ref;
  };

  // 获取服务类型列表
  getServiceDicList = () => {
    const { dispatch, location } = this.props;
    const { type } = location.query;
    dispatch({
      type: 'rebate/fetchServiceDictList',
      payload: {
        serType: type,
      },
    });
  };

  save = values => {
    const { dispatch, insUpdateVo, accuUpdateVo, location } = this.props;
    const { type, refundInfoId } = location.query;
    const updateVo = Number(type) === 1 ? insUpdateVo : accuUpdateVo;
    // let updateVo =
    // let tempType = type == 1 ? 'rebate/addInsRefundInfo' : 'rebate/addAccuRefundInfo'
    // values.sendId = empNameList[0] ? empNameList[0].sendId : values.sendId;
    // values.dataSource = empNameList[0] ? empNameList[0].dataSource : values.dataSource;
    if (values.refundDate) {
      values.refundDate = moment(values.refundDate).valueOf();
    }
    delete values.certNo;
    delete values.empName;
    if (refundInfoId) {
      delete values.sendId;
      delete values.datasource;
      dispatch({
        type: Number(type) === 1 ? 'rebate/UpdateInsInfo' : 'rebate/UpdateAccuInfo',
        payload: { ...values, refundInfoId, vno: updateVo.vno },
        callback: response => {
          if (response.success) {
            router.goBack();
          } else {
            message.error(response.message);
          }
        },
      });
    } else {
      dispatch({
        type: Number(type) === 1 ? 'rebate/addInsRefundInfo' : 'rebate/addAccuRefundInfo',
        payload: values,
        callback: response => {
          if (response.success) {
            router.goBack();
          } else {
            message.error(response.message);
          }
        },
      });
    }

    // /accuRefundInfo/addAccuRefundInfo
  };

  render() {
    const {
      empNameList,
      loading,
      serviceDictList,
      insUpdateVo,
      accuUpdateVo,
      location,
      dispatch,
    } = this.props;
    const { type } = location.query;

    return (
      // <div>
      //     <h3>派单信息</h3>

      // </div>
      <PageHeaderWrapper>
        {/* <Card> */}
        <AddTmp
          // onSearch={this.empSearch}
          empNameList={empNameList}
          loading={loading}
          empData={this.empData}
          // idIconClick={this.idIconClick}
          saveClick={this.save}
          serviceDicList={serviceDictList}
          updateVo={Number(type) === 1 ? insUpdateVo : accuUpdateVo}
          dispatch={dispatch}
        />
      </PageHeaderWrapper>
    );
  }
}

export default AddInfor;
